﻿@model IEnumerable<Task>

@{
    int selectedCategory = ViewBag.selectedCategory ?? 1;
    var currentEmpId = GimsMembership.CurrentEmpId; 
    var currentPosId = GimsMembership.User.Employee.PosId;
    var context = new GERPContext(true);

    var query = context.Tasks.Include(o => o.CreateByEmployee).Include(o => o.ApplyToEmployee).Include(o=>o.ApproveProcess).Include(o=>o.CurrentTaskHistory).Where(o1 => o1.RecordStatus != "D");
    switch (selectedCategory)
    {
        case 2: ViewBag.Descr = ViewBag.Title = "คำขอที่ตอบกลับไปแล้ว"; 
            // ดึง Task ที่มี History ที่ผ่านมาแล้ว โดยเช็คจาก Current Task History Id ไม่ให้เอาออกมา
            query = query.Where(o => o.TaskHistories.Where(o2 => o2.TaskHistoryId != o.CurrentTaskHistoryId).Any(o2 => o2.ApproverPosId == currentPosId));
            break;
            
        case 3: ViewBag.Descr = ViewBag.Title = "คำขอที่ฉันส่งไปขอทั้งหมด";
            query = query.Where(o => o.CreatedBy == currentEmpId || o.ApplyToEmpId == currentEmpId);
            break;
            
        case 4: ViewBag.Descr = ViewBag.Title = "คำขอที่ถูกตอบกลับมา"; 
            query = query.Where(o => o.CreatedBy == currentEmpId || o.ApplyToEmpId == currentEmpId);//.Where(o=>o.);
            break;
            
        case 5: ViewBag.Descr = ViewBag.Title = "คำขอที่ยกเลิกไป"; break;
        default: ViewBag.Descr = ViewBag.Title = "คำขออนุมัติที่ส่งมาทั้งหมด"; 
            // query = query.Where(o=>o.ApplyToEmpId == currentEmpId || o.TaskHistories.Any(o2=>o2.ApproverPosId == currentPosId)).
            break;
    }
    
    query = query.OrderByDescending(o3=>o3.TaskId) ;
    var data = query.ToList();
    
    if(ViewBag.NoLayout == true){Layout = "~/Views/Shared/_EmptyLayout.cshtml";}
}

@using (Html.BeginForm("DeleteAll", "MasUnit", FormMethod.Post, new {@id="form"}))
{
    @Html.AntiForgeryToken()
    <div class="row-fluid">
        @*<div class="span3">
            @Html.Partial("_InboxCategory")
        </div>*@
        
        <div class="span12">
            <div class="row-fluid">
                <div class="block-button-menu-above">
                    @*@Html.ButtonAdd()
                    @Html.ButtonDeleteAll()*@
                </div>
                @Html.ValidationSummary(true, "", new { @class = "alert alert-error" })
                <div class="row-fluid">
                    <div class="table-header span12">
                        <div class="span10">@ViewBag.Title</div>
                        @Html.Partial("_ExportButton")
                    </div>
                </div>
                
                <style>
                    .cen {
                        text-align: center;
                    }
                </style>
                <table id="table" class="table table-striped table-bordered table-hover" aria-describedby="table_info">
                    <thead>
                        <tr>
                            <th width="120px">
                                รหัสคำขอ
                            </th>
                            <th width="120px">
                                รหัสที่เกี่ยวข้อง
                            </th>
                            <th>
                                ชื่อเรื่อง
                            </th>
                            <th width="200px">
                                สร้างโดย
                            </th>
                            <th width="120px">
                                ล่าสุดเมื่อ
                            </th>
                            <th width="200px">
                                ผู้อนุมัติปัจจุบัน
                            </th>
                            <th width="120px">
                                สถานะ
                            </th>
                            <th class="center" style="width:80px;">
                                <!-- Empty head column for export buttons -->
                            </th>
                        </tr>
                    </thead>

                    <tbody>
                        @foreach(var m in data)
                        {
                            <tr>
                                <td>@m.Code</td>
                                <td style="text-align: center">@(string.IsNullOrWhiteSpace(m.RefCode) ? "-"  : m.RefCode)</td>
                                <td>
                                    <a href="@Url.Action("Details", new { Id=m.TaskId, NoLayout=ViewBag.NoLayout})">
                                        <span class='text-warning'>
                                            @m.ApproveProcess.Name
                                        </span>: @(m.Title ?? "คำขอ " + m.TaskId)
                                    </a> 
                                </td>
                                <td>@m.CreateByEmployee.FullName</td>
                            <td>@((m.UpdatedWhen ?? m.CreatedWhen).Value.ToString("d MMM yyyy"))</td>
                            @{
                                string currentApprover = "";
                                Employee employee = null;
                                    if(m.CurrentTaskHistory != null && m.CurrentTaskHistory.ApproverPosId != null && 
                                        ((employee = context.Employees.FirstOrDefault(o=>o.PosId == m.CurrentTaskHistory.ApproverPosId)) != null))
                                    {
                                        currentApprover = employee.FullName;
                                    }
                                }
                                <td>@currentApprover</td>
                                <td>
                                    
                                    @if(m.ApprovedWhen.HasValue)
                                    {
                                        @:<span class="text-success">@m.StatusTextShort</span>
                                    }
                                    else if(m.RejectedWhen.HasValue)
                                    {
                                        @:<span class="red">@m.StatusTextShort</span>
                                    }
                                    else if(m.CanceledWhen.HasValue)
                                    {
                                        @:<span class="red">@m.StatusTextShort</span>
                                    } else
                                    {
                                        <span class="text-warning">@m.StatusTextShort</span>
                                    }
                                    
                                </td>
                                <td class="td-actions">
                                    @Html.Partial("_ButtonMenuEachRow", new ExportGrid
                                        {
                                            Id=m.TaskId, 
                                            DetailsUrl = Url.Action("Details") + "/" + m.TaskId + "?nolayout=" + ViewBag.NoLayout
                                        })
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>
    </div>


    }

@section scripts {
    <script type="text/javascript">
        $(function () {
            $('#table').dataTable({
                "aaSorting": [/*[0, "desc"]*/],
                "aoColumns": [
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null
                ]
            });
        });

    </script>
}